"""
商品网站爬虫示例
"""
import scrapy
from myscraper.items import ProductItem


class ProductSpiderSpider(scrapy.Spider):
    """商品爬虫示例"""
    name = "product_spider"
    allowed_domains = ["shop.example.com"]
    start_urls = ["https://shop.example.com"]
    
    def parse(self, response):
        """解析商品列表页面"""
        self.logger.info(f"正在解析商品列表页: {response.url}")
        
        # 提取商品列表
        product_links = response.css('div.product-list a.product-link::attr(href)').getall()
        for link in product_links:
            yield response.follow(link, self.parse_product_detail)
        
        # 提取下一页链接
        next_page = response.css('a.next-page::attr(href)').get()
        if next_page:
            yield response.follow(next_page, self.parse)
    
    def parse_product_detail(self, response):
        """解析商品详情页面"""
        self.logger.info(f"正在解析商品详情页: {response.url}")
        
        product = ProductItem()
        
        # 提取商品信息
        product['url'] = response.url
        product['source'] = 'shop.example.com'
        product['name'] = response.css('h1.product-name::text').get()
        product['product_id'] = response.css('div.product-id::text').re_first(r'ID:\s*(\w+)')
        
        # 提取价格
        price_text = response.css('span.current-price::text').get()
        if price_text:
            product['price'] = float(price_text.strip().replace('¥', '').replace(',', ''))
        
        # 提取原价
        original_price_text = response.css('span.original-price::text').get()
        if original_price_text:
            product['original_price'] = float(original_price_text.strip().replace('¥', '').replace(',', ''))
        
        # 提取描述
        product['description'] = response.css('div.product-description::text').get()
        
        # 提取品牌
        product['brand'] = response.css('div.brand-info::text').get()
        
        # 提取分类
        product['category'] = response.css('div.breadcrumb span.category::text').getall()
        
        # 提取图片URL
        product['images'] = response.css('div.product-images img::attr(src)').getall()
        
        # 提取评分
        rating_text = response.css('span.rating-score::text').get()
        if rating_text:
            product['ratings'] = float(rating_text)
        
        # 提取评价数量
        reviews_count_text = response.css('span.reviews-count::text').get()
        if reviews_count_text:
            product['reviews_count'] = int(reviews_count_text.strip().replace('(', '').replace(')', ''))
        
        # 提取库存
        stock_text = response.css('span.stock::text').get()
        if stock_text:
            product['stock'] = int(stock_text.strip())
        
        return product 